﻿using DidaCode.Workflow.Logs;
using DidaCode.Workflow.Models;
using DidaCode.Workflow.Models.Parameters;
using DidaCode.Workflow.Models.Results;
using DidaCode.Workflow.NodeAttribute;
using DidaCode.Workflow.NodeBase;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DidaCode.Workflow.Nodes
{
    [NodeType(Node.RateAdjust)]
    public class RateAdjust : NodeBase<HotelSearchParameter, HotelSearchResult>
    {
        [AutoMap(Node.HotelSearch)]
        public string HotelName { get; set; }

        [AutoMap(Node.RateAdjust)]
        public RateAdjustResult RateAdjustResult { get; set; } = new RateAdjustResult();


        public RateAdjust(string switchConfig) : base(switchConfig) { }
        protected override async Task<NodeResult<HotelSearchResult>> ExecuteNodeAsync(ExecutionContext<HotelSearchParameter, HotelSearchResult> context)
        {
            await Task.Delay(100); // 模拟异步操作

            Random random = new Random();
            int randomNumber = random.Next(0, 100);
            RateAdjustResult.RateAdjustMargin = 10.0m + randomNumber;

            StringBuilder sb = new StringBuilder();
            sb.AppendLine("***************RateAdjust开始********************");
            sb.AppendLine($"[Thread: {Thread.CurrentThread.ManagedThreadId}]");
            sb.AppendLine("RateAdjust节点执行前的上下文信息:");
            sb.AppendLine($"{Node.HotelSearch}节点HotelName:{HotelName}");
            sb.AppendLine($"{Node.HotelSearch}节点HotelName:{RateAdjustResult.RateAdjustMargin}");
            sb.AppendLine("***************RateAdjust结束********************");
            Console.WriteLine(sb.ToString());

            context.Output.RateAdjustResult = RateAdjustResult.RateAdjustMargin.ToString();
            ExecutionLogger.LogResult(Node.RateAdjust, new TypedResult(typeof(string), RateAdjustResult.RateAdjustMargin.ToString()));
            if (RateAdjustResult.RateAdjustMargin > 50)
                return NodeResult<HotelSearchResult>.DefaultFailNode;
            else
                return NodeResult<HotelSearchResult>.DefaultSuccessNode;
        }
    }
}
